/*
 * cjson.h
 *
 *  Created on: Jan 22, 2025
 *      Author: hudong
 */

#ifndef CJSON_H_
#define CJSON_H_

#include <stdbool.h>
#include <stdio.h>

#pragma pack(4)
enum JSON_TYPE {
  jsonBOOL,
  jsonINTEGER,
  jsonDOUBLE,
  jsonSTRING,
  jsonOBJECT,
  jsonARRAY
};

typedef struct __json__ JSON;

typedef struct __json_value__ JSONValue;
struct __json_value__ {
  char *key;
  int reserved;
  int type;
  union {
    bool b;
    long l;
    double f;
    char *s;
    JSON *j;
  } value;
};

typedef void (*consumer_jsv)(const JSONValue *jsv, void *clouse);

struct _cjson {
  void (*init)();
  void (*destroy)();
  JSON *(*newObject)();
  JSON *(*newArray)();
  void (*free)(JSON *json);
  int (*size)(JSON *json);
  void (*addBoolByKey)(JSON *json, char *key, bool value);
  void (*addLongByKey)(JSON *json, char *key, long value);
  void (*addDoubleByKey)(JSON *json, char *key, double value);
  void (*addStringByKey)(JSON *json, char *key, char *value);
  JSON *(*addObjectByKey)(JSON *json, char *key);
  JSON *(*addArrayByKey)(JSON *json, char *key);
  void (*addBoolByIndex)(JSON *json, int index, bool value);
  void (*addLongByIndex)(JSON *json, int index, long value);
  void (*addDoubleByIndex)(JSON *json, int index, double value);
  void (*addStringByIndex)(JSON *json, int index, char *value);
  JSON *(*addObjectByIndex)(JSON *json, int index);
  JSON *(*addArrayByIndex)(JSON *json, int index);
  void (*removeByKey)(JSON *json, char *key);
  void (*removeByIndex)(JSON *json, int index);
  bool (*getBoolByKey)(JSON *json, char *key);
  long (*getLongByKey)(JSON *json, char *key);
  double (*getDoubleByKey)(JSON *json, char *key);
  char *(*getStringByKey)(JSON *json, char *key);
  bool (*getBoolByIndex)(JSON *json, int index);
  long (*getLongByIndex)(JSON *json, int index);
  double (*getDoubleByIndex)(JSON *json, int index);
  char *(*getStringByIndex)(JSON *json, int index);
  JSON *(*getJSONByKey)(JSON *json, char *key);
  JSON *(*getJSONByIndex)(JSON *json, int index);
  int (*typeOf)(JSON *json);
  JSONValue *(*valueOf)(JSON *json);
  void (*forEach)(JSON *json, consumer_jsv, void *clouse);
  int (*stringify)(JSON *json, FILE *stream);
  JSON *(*parse)(FILE *stream);
};
extern struct _cjson cjson;
#pragma pack()

#endif /* CJSON_H_ */
